Quiz 5:
Load Data:
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
data <- as.data.frame(iris)
head(data)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
Question 1:
#A
colors1 = c("blue", "red", "green")
plot <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color=Species)) +
geom_point() +
scale_color_manual(values = colors1)
# B
ggplotly(plot)
Question 2:
#a
type1 <- iris %>% filter(Species=="setosa")
#b
type2 <- iris %>% filter(Species=="versicolor")
#c
type3 <- iris %>% filter(Species=="virginica")
plot_ly(type="scatter", mode="markers") %>%
add_trace(x = type1$Petal.Length, y=type1$Petal.Width, marker=list(color="blue"), name="setosa") %>%
add_trace(x = type2$Petal.Length, y=type2$Petal.Width, marker=list(color="red"), name="versicolor") %>%
add_trace(x = type3$Petal.Length, y=type3$Petal.Width, marker=list(color="green"), name="virginica")
Question 3:
#a
type1 <- iris %>% filter(Species=="setosa")
#b
type2 <- iris %>% filter(Species=="versicolor")
#c
type3 <- iris %>% filter(Species=="virginica")
plot_ly(type="scatter", mode="markers") %>%
add_trace(x = type1$Petal.Length, y=type1$Petal.Width, marker=list(color="blue"), name="setosa"
,text = ~paste("Species: setosa", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type2$Petal.Length, y=type2$Petal.Width, marker=list(color="red"), name="versicolor"
,text = ~paste("Species: versicolor", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type3$Petal.Length, y=type3$Petal.Width, marker=list(color="green"), name="virginica"
,text = ~paste("Species: virginica", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
)
Question 4:
#a
type1 <- iris %>% filter(Species=="setosa")
#b
type2 <- iris %>% filter(Species=="versicolor")
#c
type3 <- iris %>% filter(Species=="virginica")
plot_ly(type="scatter", mode="markers") %>%
add_trace(x = type1$Petal.Length, y=type1$Petal.Width, marker=list(color="blue"), name="setosa"
,text = ~paste("Species: setosa", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type2$Petal.Length, y=type2$Petal.Width, marker=list(color="red"), name="versicolor"
,text = ~paste("Species: versicolor", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type3$Petal.Length, y=type3$Petal.Width, marker=list(color="green"), name="virginica"
,text = ~paste("Species: virginica", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
layout(title = "Scatter Plot with Custom Colors by Species"
, xaxis = list(showgrid = FALSE)
, yaxis = list(showgrid = FALSE) ) # Remove y-axis gridlines)
Question 5:
#a
type1 <- iris %>% filter(Species=="setosa")
#b
type2 <- iris %>% filter(Species=="versicolor")
#c
type3 <- iris %>% filter(Species=="virginica")
plot_ly(type="scatter", mode="markers") %>%
add_trace(x = type1$Petal.Length, y=type1$Petal.Width, marker=list(color="blue", line=list(color="black", width=2)),opacity=.5, name="setosa"
,text = ~paste("Species: setosa", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type2$Petal.Length, y=type2$Petal.Width, marker=list(color="red", line=list(color="black", width=2)),opacity=.5, name="versicolor"
,text = ~paste("Species: versicolor", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type3$Petal.Length, y=type3$Petal.Width, marker=list(color="green", line=list(color="black", width=2)),opacity=.5, name="virginica"
,text = ~paste("Species: virginica", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
layout(title = "Scatter Plot with Custom Colors by Species"
, xaxis = list(showgrid = FALSE)
, yaxis = list(showgrid = FALSE) ) # Remove y-axis gridlines)
Bonus Question
#a
type1 <- iris %>% filter(Species=="setosa")
#b
type2 <- iris %>% filter(Species=="versicolor")
#c
type3 <- iris %>% filter(Species=="virginica")
plot_ly(type="scatter3d", mode="markers") %>%
add_trace(x = type1$Petal.Length, y=type1$Petal.Width, z=type1$Sepal.Width, marker=list(color="blue", line=list(color="black", width=2)),opacity=.5, name="setosa"
,text = ~paste("Species: setosa", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type2$Petal.Length, y=type2$Petal.Width, z=type2$Sepal.Width, marker=list(color="red", line=list(color="black", width=2)),opacity=.5, name="versicolor"
,text = ~paste("Species: versicolor", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
add_trace(x = type3$Petal.Length, y=type3$Petal.Width, z=type3$Sepal.Width, marker=list(color="green", line=list(color="black", width=2)),opacity=.5, name="virginica"
,text = ~paste("Species: virginica", "Petal Length: ", type1$Petal.Length, "Petal Width: ", type1$Petal.Width)
) %>%
layout(title = "Scatter Plot with Custom Colors by Species"
, xaxis = list(showgrid = FALSE)
, yaxis = list(showgrid = FALSE) ) # Remove y-axis gridlines)
head(type1)
## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa